# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: site-packages\sqlalchemy\dialects\mssql\pymssql.py
"""
.. dialect:: mssql+pymssql
    :name: pymssql
    :dbapi: pymssql
    :connectstring: mssql+pymssql://<username>:<password>@<freetds_name>/?charset=utf8
    :url: http://pymssql.org/

pymssql is a Python module that provides a Python DBAPI interface around
`FreeTDS <http://www.freetds.org/>`_.  Compatible builds are available for
Linux, MacOSX and Windows platforms.

Modern versions of this driver work very well with SQL Server and
FreeTDS from Linux and is highly recommended.

"""
from .base import MSDialect, MSIdentifierPreparer
from ... import types as sqltypes, util, processors
import re


class _MSNumeric_pymssql(sqltypes.Numeric):
    def result_processor(self, dialect, type_):
        if not self.asdecimal:
            return processors.to_float
        else:
            return sqltypes.Numeric.result_processor(self, dialect, type_)


class MSIdentifierPreparer_pymssql(MSIdentifierPreparer):
    def __init__(self, dialect):
        super(MSIdentifierPreparer_pymssql, self).__init__(dialect)
        self._double_percents = False


class MSDialect_pymssql(MSDialect):
    supports_native_decimal = True
    driver = "pymssql"
    preparer = MSIdentifierPreparer_pymssql
    colspecs = util.update_copy(
        MSDialect.colspecs,
        {sqltypes.Numeric: _MSNumeric_pymssql, sqltypes.Float: sqltypes.Float},
    )

    @classmethod
    def dbapi(cls):
        module = __import__("pymssql")
        client_ver = tuple((int(x) for x in module.__version__.split(".")))
        if client_ver < (2, 1, 1):
            module.Binary = lambda x: x if hasattr(x, "decode") else str(x)
        if client_ver < (1,):
            util.warn(
                "The pymssql dialect expects at least the 1.0 series of the pymssql DBAPI."
            )
        return module

    def _get_server_version_info(self, connection):
        vers = connection.scalar("select @@version")
        m = re.match("Microsoft .*? - (\\d+).(\\d+).(\\d+).(\\d+)", vers)
        if m:
            return tuple((int(x) for x in m.group(1, 2, 3, 4)))
        else:
            return

    def create_connect_args(self, url):
        opts = url.translate_connect_args(username="user")
        opts.update(url.query)
        port = opts.pop("port", None)
        if port:
            if "host" in opts:
                opts["host"] = "%s:%s" % (opts["host"], port)
        return [[], opts]

    def is_disconnect(self, e, connection, cursor):
        for msg in (
            "Adaptive Server connection timed out",
            "Net-Lib error during Connection reset by peer",
            "message 20003",
            "Error 10054",
            "Not connected to any MS SQL server",
            "Connection is closed",
            "message 20006",
            "message 20017",
        ):
            if msg in str(e):
                return True
        else:
            return False

    def set_isolation_level(self, connection, level):
        if level == "AUTOCOMMIT":
            connection.autocommit(True)
        else:
            connection.autocommit(False)
            super(MSDialect_pymssql, self).set_isolation_level(connection, level)


dialect = MSDialect_pymssql
